package mobi.hami.huapin.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import mobi.hami.huapin.daobase.CrudMapper;
import mobi.hami.huapin.dto.SKUPresentDTO;
import mobi.hami.huapin.model.SkuAttribute;
import mobi.hami.huapin.model.Subject;
import mobi.hami.huapin.model.SubjectExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

@Repository
public interface SubjectMapper extends CrudMapper<Subject> {

    /**
     * 根据主题获取赠送的SKU信息
     * @param id
     * @return
     */
    @Select("select b.id,b.skuname,b.show_price,b.group_price,b.price,b.productid\n" +
            "  ,(select  replace(group_concat(s.name),',','/') as attribute_name\n" +
            "    from sku_productattribute_mapping a\n" +
            "  left join sku_attributevalue s on a.productattributevalueid=s.id\n" +
            "  where a.skuid =b.id\n" +
            "  group by a.skuid) as attribute_name\n" +
            "  ,(select max(pictuire) pictuire from sku_picture_mapping\n" +
            "    where deleted=0 and mastertag=2 and spuid =a.spu_id\n" +
            "    group by spuid) as pictuire\n" +
            "from spu_subject_mapping as a\n" +
            "LEFT join productsku b on a.spu_id=b.productid and b.deleted=0\n" +
            "where a.deleted=0  and a.subject_id=#{id}")
    List<SKUPresentDTO> getSKUPreSentListBySubjectId(Integer id);

    /**
     * 多条件搜索主题
     * @param parms
     * @return
     */
    @SelectProvider(type = SubjectProvider.class,method = "selectSubjectsWitchParam")
    List<Subject> selectSubjectsWitchParam(Map<String,Object> parms);
    /**
     * 根据id查询主题
     */
    @Select("select * from subject where id = #{id} LIMIT 1")
    Subject selectById(@Param("id") Integer id);
    /**
     * 根据id更新主题的删除状态
     * @Param id
     * @Param deleted
     */
    @Update("update subject set deleted = #{deleted},gmt_modified = #{gmt_modified} where id = #{id}")
    int updateDeletedById(@Param("id") Integer id, @Param("deleted") Integer deleted, @Param("gmt_modified") Date gmt_modified);
    /**
     * 根据主题名称查询主题
     * @param name
     * @return
     */
    @Select("select * from subject where name = #{name} LIMIT 1")
    Subject findOneByName(@Param("name")String name);

    @Update("update subject set name = #{name},use_status = #{use_status},day_of_week = #{day_of_week},description = #{description},deleted = #{deleted},gmt_modified = #{gmt_modified} where id = #{id}")
    int updateSb(Subject sb);


}